package com.mind.db.async;

import org.apache.log4j.Logger;

import com.mind.db.IEntity;
import com.mind.db.async.model.DBUpdateQueue;
public class AsyncUpdateManager {
	private static Logger log = Logger.getLogger(AsyncUpdateManager.class);
	private static final String THREAD_NAME = "database_update";
	private static final String LOG_THREAD_NAME = "log_database_update";
	private static AsyncUpdateManager threadManager = new AsyncUpdateManager();
	private  DBUpdateQueue queue=new DBUpdateQueue();
	private  DBUpdateQueue logQueue=new DBUpdateQueue();
	private Thread updateThread=new Thread(new DbUpdateThread(queue,THREAD_NAME), THREAD_NAME);
	private Thread logUpdateThread=new Thread(new DbUpdateThread(logQueue,LOG_THREAD_NAME), THREAD_NAME);
	public static AsyncUpdateManager getInstance() {
		return threadManager;
	}
	/**
	 * 启动更新线程
	 * 
	 * @param updateQueueBean
	 */
	public  void start() {
		updateThread.start();
		logUpdateThread.start();
	}
	/**
	 * 
	 */
	public void stop() {
		int count=0;
		try {
			while (queue.size() > 0 || logQueue.size() > 0) {
				count++;
				Thread.sleep(1000);
				if(count==60){
					log.info("停机超过"+count+"秒");
				}
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	public void createLog(IEntity entity){
		logQueue.create(entity);
	}
	public  void create(IEntity entity) {
		queue.create( entity);
	}
	public  void update(IEntity entity) {
		queue.update(entity);
	}
}
